//app.js
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
        }
      }
    });
    this.getSessionID();
    this.getProductType();
  },
  globalData: {
    userInfo: null,
    userRole: "0",
    webUrl: "https://dev.withwheat.wang",
    selectAddress: {
      status: -1,
      value: null
    },
    sessionID: null,
    address: null,
    coupon: {
      status: -1,
      value: null
    },
    canReqSession: true,
    canReqAddress: true
  },
  // 保留两位小数
  keepTwoDecimalFull: function (num) {
    var result = parseFloat(num);
    result = Math.round(num * 100) / 100;
    var s_x = result.toString();
    var pos_decimal = s_x.indexOf('.');
    if (pos_decimal < 0) {
      pos_decimal = s_x.length;
      s_x += '.';
    }
    while (s_x.length <= pos_decimal + 2) {
      s_x += '0';
    }
    return s_x;
  },
  getOP: function (res) {//提交用户信息 获取用户id

  },
  isEmptyObject: function (e) {
    var t;
    for (t in e)
      return !1;
    return !0
  },
  loadDataFromServer: function (urlStr, callback, paramsobj = {}, method = 'post', addheader = false) {
    urlStr = this.globalData.webUrl + urlStr;
    var header = {
      "Content-Type": "application/x-www-form-urlencoded",
    };
    if (addheader) {
      header.__sessionid = this.globalData.sessionID;
    }
    var that = this;
    wx.request({
      url: urlStr,
      header: header,
      method: method,
      data: paramsobj,
      success: function (res) {
        that.callBackCenter(callback, res.data);
      },
      fail: function (res) {
        that.callBackCenter(callback, "err");
      }
    });
  },
  callBackCenter: function (callback, data) {
    callback(data);
  },
  appShowLoading: function (duration) {
    wx.showToast({
      title: '',
      icon: 'loading',
      duration: duration
    });
  },
  formatDateTime: function (timeStamp, longflag = true) {
    var date = new Date();
    date.setTime(timeStamp * 1000);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    if (longflag == false) {
      return y + '' + m + '' + d;
    }
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
  },
  compare: function (x, y) {
    if (x < y) {
      return -1;
    } else if (x > y) {
      return 1;
    } else {
      return 0;
    }
  },
  getUserLocation: function () {
    var that = this;
    wx.getLocation({
      success: function (res) {
        that.showAddress(res.latitude, res.longitude);
      },
    });
  },
  getSessionID: function () {
    if (this.globalData.sessionID != null) {
      return;
    }
    if (this.globalData.canReqSession == false) {
      return;
    }
    this.globalData.canReqSession = false;
    var url = '/yuanmai_we/usercenter/user/info?uname=zaofans&__openid=oO--Nt86zn5GaWbH3_71EqjqtYsg';
    this.loadDataFromServer(url, this.sessionIDCallBack, {}, 'get');
  },
  sessionIDCallBack: function (res) {
    this.globalData.sessionID = res.SESSIONID;
    this.globalData.userid = res.ID;
    this.getAddress();
  },
  getAddress: function () {
    if (this.globalData.sessionID == null) {
      this.getSessionID();
      return;
    }
    if (this.globalData.address != null) {
      return;
    }
    if (this.globalData.canReqAddress == false) {
      return;
    }
    this.globalData.canReqAddress = false;
    var url = '/yuanmai_we/usercenter/address/default';
    this.loadDataFromServer(url, this.getAddressCallBack, {}, 'get', true);
  },
  getAddressCallBack: function (res) {
    console.log(res);
    this.globalData.address = res;
  },
  getProductType: function () {
    var url = '/yuanmai_we/product/productType';
    this.loadDataFromServer(url, this.getProductTypeCallBack, {}, 'get');
  },
  getProductTypeCallBack: function (res) {
    console.log(res);
    this.globalData.productType = res[0].ID;
  },
  currentDate: function () {
    var curdate = new Date();
    return this.formatDateTime(curdate.getTime() / 1000, false);
  }
})